package com.gxa.admin.recycling.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.admin.recycling.service.AdminRoleService;
import com.gxa.admin.recycling.service.RolePermissionService;
import com.gxa.admin.recycling.service.RoleService;
import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @author R
 * @data 2021-12-02 16:42
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
@Transactional(rollbackFor = Throwable.class)
public class RoleController {

    @Autowired
    RolePermissionService rolePermissionService;

    @Autowired
    RoleService roleService;

    @Autowired
    AdminRoleService adminRoleService;

    @PostMapping("/addRole")
    @ApiOperation("添加角色")
    @ApiImplicitParam(name = "map",value = "所有参数的map",required = true,dataType = "List",paramType = "body")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "name",value = "新建角色名字",required = true,dataType = "String",paramType = "query"),
//            @ApiImplicitParam(name = "remark",value = "新建角色的描述",required = true,dataType = "String",paramType = "query")
////            @ApiImplicitParam(name = "oids",value = "该角色对应勾选的所有权限的id",required = true,dataType = "List",paramType = "query")
//    })String name, String remark,@ApiParam(value = "该角色对应勾选的所有权限的id")  List<Integer> oids
    public Map addRole(@RequestBody Map<String,Object> map){
        System.out.println(map);
        Object name = map.get("name");
        Object remark = map.get("remark");
        Role role = new Role();
        role.setName(name.toString());
        role.setRemark(remark.toString());
        role.setAddTime(new Timestamp(System.currentTimeMillis()));
        roleService.insert(role);
        System.out.println(role);
//        List<Integer> oid1 = new ArrayList<>();
//        for (String oid : oids) {
//            oid1.add(Integer.parseInt(oid));
//        }
        List<Integer> oids= (List)map.get("oids");
        rolePermissionService.insert(Integer.parseInt(role.getId().toString()),oids);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("添加成功");
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getAll")
    @ApiOperation("查询全部角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,dataType = "Integer",paramType = "query"),
    })
    public Map getAll(Integer page,Integer size) throws IllegalAccessException {
        PageInfo<Role> rolePageInfo = roleService.selectAllByPage(page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(rolePageInfo.getList());
        jsonResult.put("count",rolePageInfo.getTotal());
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteRole")
    @ApiOperation("删除角色")
    @ApiImplicitParam(name = "id",value = "需要删除的角色对应的id")
    public Map deleteRole(Long id) throws BusinessException {
        Role role = roleService.selectById(id);
        if (role.getName() != null && role.getName().equals("超级管理员")){
            throw new BusinessException(ExceptionCode.CAN_NOT_UPDATE);
        }
        List<Integer> contact = adminRoleService.getContact(id);
        if (contact.size() > 0){
            throw new BusinessException(ExceptionCode.DELETE_ROLE_FAILED);
        }
        Integer integer = roleService.deleteById(id);
        rolePermissionService.deleteByRid(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getAllRoleNoPage")
    @ApiOperation("添加管理员的时候分配角色使用")
    public Map getAllRoleNoPage(){
        List<Role> roles = roleService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("查询成功");
        jsonResult.setCode("200");
        jsonResult.setData(roles);
        return jsonResult.getDataMap();
    }

    @PostMapping("/updateRole")
    @ApiOperation("修改角色")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "rid",value = "需要修改的角色id",required = true,dataType = "Long",paramType = "query"),
//            @ApiImplicitParam(name = "mids",value = "勾选的权限id的集合或者数组",dataType = "List",required = true,paramType = "query")
//    })
    @ApiImplicitParam(name = "map",value = "所有参数的map",required = true,paramType = "body")

    public Map updateRole(@RequestBody Map<String,Object> map) throws BusinessException {
        Role role = roleService.selectById(Integer.parseInt(map.get("rid").toString()));
        if (role == null){
            throw new BusinessException(ExceptionCode.ROLE_NOT_EXIST);
        }
        if (role.getName().equals("超级管理员")){
            throw new BusinessException(ExceptionCode.CAN_NOT_UPDATE);
        }
        Object remark = map.get("remark");
        Role role1 = new Role();
        role1.setId(Long.parseLong(map.get("rid").toString()));
        role1.setRemark(remark.toString());
        System.out.println(role1);
        Integer integer = roleService.updateById(role1);
        rolePermissionService.deleteByRid(Long.parseLong(map.get("rid").toString()));
//        List<Integer> mid1 = new ArrayList<>();
//        for (String mid : mids) {
//            mid1.add(Integer.parseInt(mid));
//        }
        List<Integer> mids = (List)map.get("mids");
        rolePermissionService.insert(Integer.parseInt(map.get("rid").toString()),mids);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("修改成功");
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/findById")
    @ApiOperation("根据ID查询角色信息")
    @ApiImplicitParam(name = "id",value = "角色对应id",required = true,dataType = "Long",paramType = "query")
    public Map findById(Long id){
        Role role = roleService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(role);
        return jsonResult.getDataMap();
    }

}
